﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TamTreSoSinh.DBContext;
using TamTreSoSinh.Enum;

namespace TamTreSoSinh.UC
{
    public partial class KnownledgePart : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            DBTamTreEntities dbContext = new DBTamTreEntities();
            LoadKnowledge(dbContext);
            LoadNews(dbContext);
        }

        private void LoadKnowledge(DBTamTreEntities dbContext)
        {
            List<Knownledge> listKnowledge = dbContext.Knownledges.Where(obj => obj.IsOnHomePage && obj.KnownledgeCategoryId == (int)KnowledgeEnum.Knowledge).OrderBy(obj => obj.Priority).ToList();
            ShowFirstKnowledge(dbContext);

            rptKnowledgeList.DataSource = listKnowledge;
            rptKnowledgeList.DataBind();
        }

        private void LoadNews(DBTamTreEntities dbContext)
        {
            List<Knownledge> listNews = dbContext.Knownledges.Where(obj => obj.IsOnHomePage && obj.KnownledgeCategoryId == (int)KnowledgeEnum.News).OrderBy(obj => obj.Priority).ToList();
            ShowFirstNews(dbContext);

            rptNewstList.DataSource = listNews;
            rptNewstList.DataBind();
        }

        private void ShowFirstKnowledge(DBTamTreEntities dbContext)
        {
            var firstKnowledge = dbContext.Knownledges.Where(obj => obj.IsOnHomePage && obj.KnownledgeCategoryId == (int)KnowledgeEnum.Knowledge)
                                            .OrderBy(obj => obj.Priority).FirstOrDefault();

            var url = "/KnownledgeDetailPage.aspx?Id=" + firstKnowledge.Id;
            hplImgKnowledgeFist.ImageUrl = firstKnowledge.MainImage;
            hplImgKnowledgeFist.NavigateUrl = url;

            hplHeadKnowledgeFirst.Text = firstKnowledge.Title;
            hplHeadKnowledgeFirst.NavigateUrl = url;

            lblContentKnowledgeFirst.Text = firstKnowledge.KnownledgeContent;
        }

        protected void rptKnowledgeList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HyperLink hpl;
            Knownledge knowledge = (Knownledge)e.Item.DataItem;
            hpl = (HyperLink)e.Item.FindControl("hplTitleKnowledgeList");
            hpl.Text = knowledge.Title;
            hpl.NavigateUrl = "/KnownledgeDetailPage.aspx?Id=" + knowledge.Id;
        }

        private void ShowFirstNews(DBTamTreEntities dbContext)
        {
            var firstNews = dbContext.Knownledges.Where(obj => obj.IsOnHomePage && obj.KnownledgeCategoryId == (int)KnowledgeEnum.News)
                                            .OrderBy(obj => obj.Priority).FirstOrDefault();

            var url = "/KnownledgeDetailPage.aspx?Id=" + firstNews.Id;
            hplImgNewsFirst.ImageUrl = firstNews.MainImage;
            hplImgNewsFirst.NavigateUrl = url;

            hplHeadNewsFirst.Text = firstNews.Title;
            hplHeadNewsFirst.NavigateUrl = url;

            lblContentNewsFirst.Text = firstNews.KnownledgeContent;
        }

        protected void rptNewstList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HyperLink hpl;
            Knownledge knownledge = (Knownledge)e.Item.DataItem;
            hpl = (HyperLink)e.Item.FindControl("hplTitleNewsList");
            hpl.Text = knownledge.Title;
            hpl.NavigateUrl = "/KnownledgeDetailPage.aspx?Id=" + knownledge.Id;
        }
    }
}